using UnityEngine;
using System.Collections;

public class FadeIn : MonoBehaviour 
{
	private GameObject texture;
	private Texture2D textureFade;
	private float count;
	private float amount;
	private bool isRun;
	
	public void createFade(float amount)
	{
		this.amount = amount;
		
		texture = new GameObject();
		texture.name = "Fade In";
		texture.AddComponent("GUITexture");
		texture.guiTexture.pixelInset = new Rect(0,0,Screen.width,Screen.height);
		textureFade = new Texture2D(1,1);
		textureFade.SetPixels(new Color[1] {Color.black});
		textureFade.Apply();
		texture.guiTexture.color = new Color(0,0,0,1);
		texture.guiTexture.texture = textureFade;
		
		count = 1;
		isRun = true;
	}
	
	public bool existFade()
	{
		return !isRun;
	}
	
	void Update() 
	{

		if(isRun)	
		{
			count -= amount * Time.deltaTime;
			
			texture.guiTexture.color = new Color(0,0,0,count);
	
			if(count <= 0)
			{
				Destroy(texture);
				DestroyObject(this);
			}
		}
	}
}
